<?php
namespace Idiom\Controller;
use Common\Controller\IdiomController;
class WxpayController extends IdiomController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     * 微信支付
     */
    public function wxpay(){

        $member_id = I('post.member_id','','int');
        $total_fee = I('post.total_fee','','htmlspecialchars');
        $openid = I('post.openid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');



        if ($member_id==""||$total_fee==""||$appid==""||$openid==""){
            $this->returnData('','参数错误',401);
        }


        $total_fee = $total_fee*100;
       // $total_fee = $total_fee;

        $wxpay  = new \Idiom\Model\WxpayhistoryModel($appid);

        $body = '猜成语支付答题';
        $order = $wxpay->setout_trade_no();
        $params = array(
            'body'=>$body,
            'out_trade_no'=>$order,
            'total_fee'=>$total_fee,
            'trade_type'=>'JSAPI',
            'openid'=>$openid,
        );


        $res = $wxpay->unifiedOrder2( $params );

        if ($res[return_code]!='SUCCESS'){
            $this->returnData('','失败',100);
        }

        $timeStamp = time();
        $data = array();

        $data['appId'] = $res[appid];
        $data['timeStamp'] = "$timeStamp";
        $data['nonceStr'] = $wxpay->genRandomString();
        $data['package'] = 'prepay_id='.$res[prepay_id];
        $data['signType'] = 'MD5';
        $data['paySign'] = $wxpay->MakeSign($data);
        $data['prepay_id'] = $res[prepay_id];

        $arr = array(
            'appid'=>$res[appid],
            'mch_id'=>$res[mch_id],
            'body'=>$body,
            'out_trade_no'=>$order,
            'total_fee'=>$total_fee,
            'idiom_member_id'=>$member_id,
            'ispay'=>0,
            'prepay_id'=>$res[prepay_id],
            'createtime'=>date('Y-m-d H:i:s'),
        );

        M('idiom_wxpayhistory')->data($arr)->add();
        $this->returnData($data,'成功',200);

    }

    /**
     * 更新支付状态
     */
    public function chekpay(){

        $member_id = I('post.member_id','','int');
        $ispay = I('post.ispay','0','int');
        $appid = I('post.appid','','htmlspecialchars');
        $prepay_id = I('post.prepay_id','','htmlspecialchars');
        $money = I('post.money','','htmlspecialchars');

        if (!$member_id||!$ispay){
            $this->returnData('','参数错误',401);
        }

        $where['idiom_member_id'] = $member_id;
        $where['appid'] = $appid;
        $where['prepay_id'] = $prepay_id;
        $where['ispay'] = '0';

        $id = "";
        $total_fee = M('idiom_wxpayhistory')->where($where)->getField('total_fee');

        if ($total_fee==""){
            $this->returnData('','没有订单',200);
        }

        $data = array(
            'ispay'=>$ispay,
            'updatetime'=>date('Y-m-d H:i:s'),
        );

        M('idiom_wxpayhistory')->where($where)->save($data);

        if ($money=='0.5'){
            $this->returnData(array('resurgence'=>'1'),'支付成功',200);
        }

        if ($money=='1'){
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('opportunity'); //挑战机会加一

            $this->returnData(array('num'=>'1'),'支付成功',200);
        }

        if ($money=='5'){
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('opportunity',5); //挑战机会加5
            $this->returnData(array('num'=>'5'),'支付成功',200);
        }

        if ($money=='8'){
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('opportunity',10); //挑战机会加10
            $this->returnData(array('num'=>'10'),'支付成功',200);
        }

    }


}
